package com.jiezuo.flowable.listener;

import cn.hutool.core.collection.CollectionUtil;
import com.jiezuo.common.utils.SpringUtil;
import com.jiezuo.flowable.mapper.CommonMapper;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.impl.context.Context;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @description 事业部院长监听器
 * @author 庞留杰
 * @date 2025/2/22 17:34
 * @version V1.0
 **/
@Slf4j
@Component
public class BusinessDepartDeanListener implements ExecutionListener {

    @Autowired
    CommonMapper commonMapper;

    @Override
    public void notify(DelegateExecution execution) {
        String processInstanceId = execution.getProcessInstanceId();
        CommonMapper commonMapper = SpringUtil.getObject(CommonMapper.class);
        RuntimeService runtimeService = Context.getProcessEngineConfiguration().getRuntimeService();
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        String startUserId = processInstance.getStartUserId();//申请人
        // 各事业部院长
        List<String> list = commonMapper.findBusinessDepartDeanByUserId(startUserId);
        if (!CollectionUtil.isEmpty(list)) {
            String join = String.join(",", list);
            execution.setVariable("dean", join);
        }
    }

}
